#coding=utf-8
#Boa:Frame:FrameDeliveryRoute

import wx

from MyListCtrl import *

def create(parent):
    return FrameDeliveryRoute(parent)

[wxID_FRAMEDELIVERYROUTE, wxID_FRAMEDELIVERYROUTEBTNADD, 
 wxID_FRAMEDELIVERYROUTEBUTTON_ALL_REGION, 
 wxID_FRAMEDELIVERYROUTECHECKBOX_ONLY_SHOP, 
] = [wx.NewId() for _init_ctrls in range(4)]

class FrameDeliveryRoute(wx.Frame):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Frame.__init__(self, id=wxID_FRAMEDELIVERYROUTE, name='',
              parent=prnt, pos=wx.Point(52, 0), size=wx.Size(1066, 646),
              style=wx.DEFAULT_FRAME_STYLE, title=u'\u9001\u8d27\u8def\u7ebf')
        self.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.BOLD, False,
              u'\u6977\u4f53_GB2312'))
        self.SetBackgroundColour(wx.Colour(183, 209, 173))
        self.SetClientSize(wx.Size(1050, 608))
        self.Bind(wx.EVT_CLOSE, self.OnFrameDeliveryRouteClose)

        self.btnAdd = wx.Button(id=wxID_FRAMEDELIVERYROUTEBTNADD,
              label=u'\u65b0\u589e\u8def\u7ebf', name=u'btnAdd', parent=self,
              pos=wx.Point(8, 8), size=wx.Size(93, 26), style=0)
        self.btnAdd.Bind(wx.EVT_BUTTON, self.OnBtnAddButton,
              id=wxID_FRAMEDELIVERYROUTEBTNADD)

        self.checkBox_only_shop = wx.CheckBox(id=wxID_FRAMEDELIVERYROUTECHECKBOX_ONLY_SHOP,
              label=u'\u53ea\u663e\u793a\u4e94\u91d1\u5e97\u5ba2\u6237',
              name=u'checkBox_only_shop', parent=self, pos=wx.Point(300, 262),
              size=wx.Size(168, 16), style=0)
        self.checkBox_only_shop.SetValue(True)
        self.checkBox_only_shop.Bind(wx.EVT_CHECKBOX,
              self.OnCheckBox_only_shopCheckbox,
              id=wxID_FRAMEDELIVERYROUTECHECKBOX_ONLY_SHOP)

        self.button_all_region = wx.Button(id=wxID_FRAMEDELIVERYROUTEBUTTON_ALL_REGION,
              label=u'\u5168\u90e8\u533a\u57df', name=u'button_all_region',
              parent=self, pos=wx.Point(8, 258), size=wx.Size(160, 26),
              style=0)
        self.button_all_region.Bind(wx.EVT_BUTTON,
              self.OnButton_all_regionButton,
              id=wxID_FRAMEDELIVERYROUTEBUTTON_ALL_REGION)

    def __init__(self, parent):
        self._init_ctrls(parent)

        self.lcRoute = MyListCtrlRoute(self)
        self.lcRoute.SetSize(wx.Size(1000, 200))
        self.lcRoute.Move(wx.Point(8, 40))
        self.lcRoute.refresh()
        self.lcRoute.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnRouteItemSelected)
        
        self.lcRegion = MyListCtrlRouteDetail(self)
        self.lcRegion.Move(wx.Point(8, 290))
        self.lcRegion.SetSize(wx.Size(280, 300))
        self.lcRegion.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnRegionItemSelected)
        self.lcRegion.refresh()

        self.lcCustomer = MyCustomerListCtrl(self)
        self.lcCustomer.Move(wx.Point(300, 290))
        self.lcCustomer.SetSize(wx.Size(700, 300))

        self.button_all_region.SetLabel("ALL ("+str(self.lcRegion.m_active_customer_count)+"/"+str(self.lcRegion.m_customer_count)+")")
        
    def OnBtnAddButton(self, event):
        import DlgRouteEditor
        
        dlg = DlgRouteEditor.create(self)
        if (dlg.ShowModal() != wx.ID_OK):
            return
        
        self.lcRoute.refresh()

    def OnButton_all_regionButton(self, event):
        self.lcRegion.refresh()
    
    def OnRouteItemSelected(self, event):
    
        item = self.lcRoute.GetNextItem(-1, state = wx.LIST_STATE_SELECTED)
        if (item == -1):
            return
        
        routeId = int(self.lcRoute.GetItem(item, self.lcRoute.iId).GetText())

        self.lcRegion.refresh({'routeId':routeId})
    
    def OnRegionItemSelected(self, event):
        item = self.lcRegion.GetNextItem(-1, state = wx.LIST_STATE_SELECTED)
        regionId = self.lcRegion.getRegionId(item)
        self.lcCustomer.refresh({
            'regionId':regionId,
            'only_wujindian': self.checkBox_only_shop.GetValue()
        })

    def OnCheckBox_only_shopCheckbox(self, event):

        self.OnRegionItemSelected(event)
        
    def OnFrameDeliveryRouteClose(self, event):
        self.Show(False)
            
